package fi.iki.joker.mobilogger;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import fi.iki.joker.mobilogger.db.DBUtil;
import fi.iki.joker.mobilogger.db.SettingBean;
import fi.iki.joker.mobilogger.util.AlertUtil;

/**
 * This activity class allows user to edit and set the
 * global settings of this application.
 * 
 * The settings are not really used yet so this is more like a GUI mockup.
 * The biggest work here is probably the way he different types or attributes are handled.
 * I.e. String and other "free form" attributes can just be entered, spell checked etc,
 * but for "enumerated" types you need a spinner. This is not implemented yet.
 * 
 * @author juha
 *
 */

public class MobiLoggerSettings extends Activity implements
	View.OnClickListener {

	private DBUtil db = null;
	private SettingBean[] settings = null;
	private EditText [] editPtrs = null;
	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		setContentView(R.layout.settings);
		LinearLayout ll = (LinearLayout)findViewById(R.id.settingslayout);
		db = new DBUtil();
		if(db != null) {
			settings = db.getSavedSettings(this);
		}
		editPtrs = new EditText[settings.length];
		if(settings != null) {
			for(int i = 0; i < settings.length; i++) {
				TextView tv = new TextView(this);
				tv.setText(settings[i].getSettingtitle());
				ll.addView(tv,new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.FILL_PARENT,
	                    LinearLayout.LayoutParams.WRAP_CONTENT));
				editPtrs[i] = new EditText(this);
				editPtrs[i].setText(settings[i].getSettingvalue());
				ll.addView(editPtrs[i],new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.FILL_PARENT,
	                    LinearLayout.LayoutParams.WRAP_CONTENT));
			}
			Button b = new Button(this);
			b.setText("Save settings");
			b.setOnClickListener(this);
			ll.addView(b, new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
		} else {
			AlertUtil.showAlert(this,"Can't load settings",
					"Unable to load the settings from database!");
			finish();
			
		}
	}
	
	public void onClick(View arg0) {
		for(int i = 0; i < editPtrs.length; i++) {
			settings[i].setSettingvalue(editPtrs[i].getText().toString());
			db.update(this,settings[i]);
		}
	}
	
	
	
}
